//
//  AppDelegate.swift
//  DSWeibo
//
//  Created by liuzhu on 2018/10/27.
//  Copyright © 2018 liuzhu. All rights reserved.
//

import UIKit

//切换根控制器通知
let XMGSwitchRootViewControllerKey = "XMGSwitchRootViewControllerKey"

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?   //默认值是nil
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        //注册一个通知
        NotificationCenter.default.addObserver(self, selector: #selector(switchRootViewController), name: NSNotification.Name(rawValue: XMGSwitchRootViewControllerKey), object: nil)
        
        //设置导航条和工具条的全局外观
        UINavigationBar.appearance().tintColor = UIColor.orange
        UITabBar.appearance().tintColor = UIColor.orange
        
        //1创建window
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.backgroundColor = UIColor.white     //？如果有值就相应后面的方法，nil就不响应，！确定一定有值，没值就崩溃
        
        //2.创建根控制器
        window?.rootViewController = defaultController()
        window?.makeKeyAndVisible()
        return true
    }
    
    @objc private func switchRootViewController(notify:NSNotification){
        window?.rootViewController = isNewUpdate() ? BBFeatureCollectionViewController():MainViewController()
    }
    
    private func defaultController()->UIViewController{
        if UserAccount.userLogin(){
            //已经登录
           return isNewUpdate() ? BBFeatureCollectionViewController():MainViewController()
        }
        return MainViewController()
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }

    private func isNewUpdate() ->Bool{
        //获取当前软件的版本号
        let currentVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as!String
        //获取以前的软件版本号
        let sandboxVersion = UserDefaults.standard.object(forKey: "CFBundleShortVersionString") as?String ?? ""
        //比较
        if currentVersion.compare(sandboxVersion) == ComparisonResult.orderedDescending{
            
            UserDefaults.standard.set(currentVersion, forKey: "CFBundleShortVersionString")
            return true
        }
        return false
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
}

